﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Special;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess
{
    internal class CmsEntityMetaGetInformationDataCommand : CmsEntityMetaGetInformationCommand
    {
        public override CmsEntityMetaInformation ExecuteOne()
        {
            CmsEntityMetaInformation result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsEntityMetaInformation>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsEntityMetaInformation>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsEntityMetaInformation, int>>();

                result = SqlUtility<CmsEntityMetaInformation, int>.ExecuteMultiReader(
                    currentUnitOfWork.Connection,
                    currentUnitOfWork.Transaction,
                    "cms_sp_EntityMetaSelectFor",
                    new
                    {
                        EntityId = CommandParameter.EntityId,
                        EntityTypeParameter = CommandParameter.EntityTypeKey
                    },
                    mapper);
            }

            return result;
        }
    }
}
